package servlet;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import dao.entity.BossDaoImpl;
import dao.entity.CustomerDaoImpl;
import dao.entity.DeveloperDaoImpl;
import dao.entity.ServiceDaoImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String identity=request.getParameter("identity");
        switch (identity)
        {
            case "customer":
                loginCustomer(request,response);
                break;
            case "service":
                loginService(request,response);
                break;
            case "developer":
                loginDeveloper(request,response);
                break;
            case "boss":
                loginBoss(request,response);
                break;
        }
    }

    private void loginCustomer(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        String name=request.getParameter("cname");//参数名我们要各自区分，因此有cname,sname,devname,bname这样的实质上相同参数名却不同的变量
        String pwd=request.getParameter("pwd");

        CustomerDaoImpl cdi=new CustomerDaoImpl();
        boolean is_online=cdi.isOnline(name);
        if(is_online){
            //您已登录，请勿重复操作-0
            request.setAttribute("error_code",0);
            request.getRequestDispatcher("user_login.jsp").forward(request,response);
            return;
        }
        boolean is_exist=cdi.userLogin(name,pwd);
        if(is_exist){
            //登录成功-1
            System.out.println("用户登录成功!");
            request.setAttribute("error_code",1);//无误
            request.setAttribute("msg","登录成功");
            ServletContext context=getServletContext();
            context.setAttribute("cname",name);
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }else{
            //登录失败-用户名或密码错误-2
            request.setAttribute("error_code", 2);
            request.getRequestDispatcher("user_login.jsp").forward(request,response);
            return;
        }
    }
    private void loginService(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        String name=request.getParameter("sname");
        String pwd=request.getParameter("pwd");

        ServiceDaoImpl sdi=new ServiceDaoImpl();
        boolean is_online=sdi.isOnline(name);
        if(is_online){
            //您已登录，请勿重复操作-0
            request.setAttribute("error_code",0);
            request.getRequestDispatcher("service_login.jsp").forward(request,response);
            return;
        }
        boolean is_exist=sdi.serviceLogin(name,pwd);
        if(is_exist){
            //登录成功-1
            System.out.println("客服登录成功!");
            request.setAttribute("error_code",1);//无误
            request.setAttribute("msg","登录成功");
            ServletContext context=getServletContext();
            context.setAttribute("sname",name);
            request.getRequestDispatcher("service_platform.jsp").forward(request,response);
        }else{
            //登录失败-用户名或密码错误-2
            request.setAttribute("error_code", 2);
            request.getRequestDispatcher("service_login.jsp").forward(request,response);
            return;
        }
    }
    private void loginDeveloper(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        String name=request.getParameter("devname");
        String pwd=request.getParameter("pwd");

        DeveloperDaoImpl ddi=new DeveloperDaoImpl();
        boolean is_online=ddi.isOnline(name);
        if(is_online){
            //您已登录，请勿重复操作-0
            request.setAttribute("error_code",0);
            request.getRequestDispatcher("dev_login.jsp").forward(request,response);
            return;
        }
        boolean is_exist=ddi.developerLogin(name,pwd);
        if(is_exist){
            //登录成功-1
            System.out.println("开发者登录成功!");
            request.setAttribute("error_code",1);//无误
            request.setAttribute("msg","登录成功");
            ServletContext context=getServletContext();
            context.setAttribute("devname",name);
            request.getRequestDispatcher("dev_platform.jsp").forward(request,response);
        }else{
            //登录失败-用户名或密码错误-2
            request.setAttribute("error_code", 2);
            request.getRequestDispatcher("dev_login.jsp").forward(request,response);
            return;
        }
    }
    private void loginBoss(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        String name=request.getParameter("bname");
        String pwd=request.getParameter("pwd");

        BossDaoImpl bdi=new BossDaoImpl();
        boolean is_online=bdi.isOnline(name);
        if(is_online){
            //您已登录，请勿重复操作-0
            request.setAttribute("error_code",0);
            request.getRequestDispatcher("boss_login.jsp").forward(request,response);
            return;
        }
        boolean is_exist=bdi.bossLogin(name,pwd);
        if(is_exist){
            //登录成功-1
            System.out.println("Boss登录成功!");
            request.setAttribute("error_code",1);//无误
            request.setAttribute("msg","登录成功");
            ServletContext context=getServletContext();
            context.setAttribute("bname",name);
            request.getRequestDispatcher("boss_platform.jsp").forward(request,response);
        }else{
            //登录失败-用户名或密码错误-2
            request.setAttribute("error_code", 2);
            request.getRequestDispatcher("boss_login.jsp").forward(request,response);
            return;
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        super.doPost(req, resp);
    }
}
